﻿// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证（版本 2.0）进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动！任何基于本项目二次开发而产生的一切法律纠纷和责任，我们不承担任何责任！

using Admin.NET.Plugin.Easy.Wms.IServices.IWareHouse;
using Admin.NET.Plugin.Easy.Wms.Services.WareHouse.DTO;
using Admin.NET.Plugin.Easy.Wms.Util;
using Furion.FriendlyException;
using Mapster;

namespace Admin.NET.Plugin.Easy.Wms.Services.WareHouse;

/// <summary>
/// 仓库管理服务
/// </summary>
internal class WareHouseServices : BaseService.BaseServices<Easy_Wms_WareHouseEntity>, IWareHouseServices
{
    private readonly SqlSugarRepository<Easy_Wms_WareHouseEntity> _warehouse = App.GetService<SqlSugarRepository<Easy_Wms_WareHouseEntity>>();

    /// <summary>
    /// 添加仓库业务逻辑
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Add(WareHouseAddInput input)
    {
        input.WareHouseNum = input.WareHouseNum.GenerateIfNullOrEmpty(() => GenerateNum.GeneratedNumber("WH"));
        var entity = input.Adapt<Easy_Wms_WareHouseEntity>();
        await _warehouse.InsertAsync(entity);
    }

    /// <summary>
    /// 删除仓库业务逻辑
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    /// TODO：未添加删除仓库校验
    public async Task Delete(WareHouseDeleteInput input)
    {
        var entity = await _warehouse.GetFirstAsync(x => x.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _warehouse.FakeDeleteAsync(entity);
    }

    /// <summary>
    /// 分页查询仓库信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public async Task<SqlSugarPagedList<WareHouseOutPut>> PageAsync(WareHousePageInput input)
    {
        var entity = await _warehouse.AsQueryable()
                               .WhereIF(!string.IsNullOrEmpty(input.WareHouse_Num), x => x.WareHouse_Num.Contains(input.WareHouse_Num))
                               .WhereIF(!string.IsNullOrEmpty(input.WareHouse_Name), x => x.WareHouse_Name.Contains(input.WareHouse_Name))
                               .WhereIF(input.Id > 0, x => x.Id == input.Id)
                               //.WhereIF(!string.IsNullOrEmpty(input.WareHouse_Type.Value.ToString()), x => input.WareHouse_Type.Value.ToString().Contains(x.WareHouse_Type))
                               .Select<WareHouseOutPut>()
                               .ToListAsync();

        return entity.ToPagedList(input.Page, input.PageSize);
    }

    /// <summary>
    /// 修改仓库业务逻辑
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    /// TODO：还未添加仓库修改校验
    public async Task Update(WareHouseUpdateInput input)
    {
        var model = input.Adapt<Easy_Wms_WareHouseEntity>();
        model.UpdateTime = DateTime.Now;
        await _warehouse.AsUpdateable(model).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }
}
